package com.vviton.zyhj.admin.servlet;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.client.RestTemplate;

import com.vviton.zyhj.common.base.AppConfig;
import com.vviton.zyhj.common.content.SpringContext;

/**
 * ClassName: ToBatchTrans <br/>
 * Function: 批量打款到支付宝接口接入. <br/>
 * Date: 2015年11月9日 <br/>
 *
 * @author zhangjin
 * @version 1.0
 * @since JDK 1.7
 */
@WebServlet("/ToBatchTrans")
public class ToBatchTrans extends HttpServlet {

    /** 序列号 */
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ToBatchTrans() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * doGet,批量打款到支付宝get 请求. <br/>
     * Author: zhangjin <br/>
     * Create Date: 2016年2月1日 <br/>
     * =============================================================== <br/>
     * Modifier: huangqq <br/>
     * Modify Date: 2016年2月1日 <br/>
     * Modify Description: <br/>
     * =============================================================== <br/>
     *
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws ServletException 异常
     * @throws IOException 异常
     * @since JDK 1.7
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * doPost,批量打款到支付宝post 请求. <br/>
     * Author: zhangjin <br/>
     * Create Date: 2016年2月1日 <br/>
     * =============================================================== <br/>
     * Modifier: huangqq <br/>
     * Modify Date: 2016年2月1日 <br/>
     * Modify Description: <br/>
     * =============================================================== <br/>
     *
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws ServletException 异常
     * @throws IOException 异常
     * @since JDK 1.7
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //        PlfkdddygxService plfkdddygxService = SpringContext.getBean(PlfkdddygxService.class);
        //        ZjzfxxPtService zjzfxxPtService = SpringContext.getBean(ZjzfxxPtService.class);
        //        ZfbzhService zfbzhService = SpringContext.getBean(ZfbzhService.class);

        double batchFee = 0.0; //付款总金额
        int batchNum = 0; //付款笔数
        StringBuffer detailData = new StringBuffer(); //付款详细数据
        Date now = new Date();
        String batchNo = new SimpleDateFormat("yyyyMMddHHmmss").format(now); //批次号
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyyMMdd");
        String payDate = myFmt.format(now); //付款当天日期

        DecimalFormat df = new DecimalFormat("#.##");
        String ktid = request.getParameter("ktid");
        Map<String, Double> jsxxMap = new HashMap<String, Double>();
        String[] jsxxs = request.getParameterValues("jsxx");
        if (jsxxs != null && jsxxs.length > 0) {
            for (String jsxx : jsxxs) {
                String[] infos = jsxx.split(",");
                /*
                 * 0-id(结算订单的id)
                 * 1-jsje
                 * 2-jsfmc
                 */
                if (infos != null && infos.length > 0) {
                    if (!jsxxMap.containsKey(infos[0])) {
                        jsxxMap.put(infos[0], Double.parseDouble(infos[1]));
                    } else {
                        jsxxMap.put(infos[0], jsxxMap.get(infos[0]) + Double.parseDouble(infos[1]));
                    }
                }
            }
        }
        if (jsxxMap != null && jsxxMap.size() > 0) {
            for (Entry<String, Double> en : jsxxMap.entrySet()) {
                System.out.println("Map的键值为：en.getKey()-----:" + en.getKey());
                double ysje = Double.parseDouble(df.format(en.getValue().doubleValue()));
                if (ysje > 0) {
                    batchNum++;
                    batchFee = batchFee + ysje;
                    AppConfig appConfig = SpringContext.getBean(AppConfig.class);
                    RestTemplate template = new RestTemplate();
                    String url = appConfig.getUrlGetKtZfbInfo();
                    String zfbxx = template.getForObject(appConfig.getUrlGetKtZfbInfo() + ktid, String.class);
                    if (zfbxx != null) {
                        detailData.append(en.getKey() + "^" + zfbxx + "^" + ysje + "^付款到商家|");
                    } else {
                        detailData.append(en.getKey() + "^18896965910^朱传宝^" + ysje + "^付款到商家|");
                    }
                }
            }
        }

        request.setAttribute("extend_param", "");
        request.setAttribute("batch_fee", df.format(batchFee));
        request.setAttribute("batch_num", batchNum + "");
        request.setAttribute("batch_no", batchNo);
        request.setAttribute("pay_date", payDate);
        request.setAttribute("detail_data", detailData.toString().substring(0, detailData.toString().length() - 1));
        request.getRequestDispatcher("/alipay/alipayapi.jsp").forward(request, response);
    }
}
